#include "/home/cdd/day5/func.h"
// 定义商品结构体
typedef struct Product {
    int id;
    struct Product* next;
} Product;
// 定义链式队列结构体
typedef struct Queue {
    Product* head;
    Product* tail;
    int size;
} Queue;
// 初始化队列
Queue* createQueue() {
    Queue* queue = (Queue*)malloc(sizeof(Queue));
    queue->head = NULL;
    queue->tail = NULL;
    queue->size = 0;
    return queue;
}
// 入队操作
void enqueue(Queue* queue, int id) {
    Product* newProduct = (Product*)malloc(sizeof(Product));
    newProduct->id = id;
    newProduct->next = NULL;
    if (queue->tail == NULL) {
        queue->head = newProduct;
        queue->tail = newProduct;
    } else {
        queue->tail->next = newProduct;
        queue->tail = newProduct;
    }
    queue->size++;
}
// 出队操作
int dequeue(Queue* queue) {
    if (queue->head == NULL) {
        return -1;
    }
    Product* temp = queue->head;
    int id = temp->id;
    queue->head = queue->head->next;
    if (queue->head == NULL) {
        queue->tail = NULL;
    }
    free(temp);
    queue->size--;
    return id;
}
// 销毁队列
void destroyQueue(Queue* queue) {
    Product* current = queue->head;
    Product* next;
    while (current!= NULL) {
        next = current->next;
        free(current);
        current = next;
    }
    free(queue);
}

// 互斥锁
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
// 条件变量
pthread_cond_t empty = PTHREAD_COND_INITIALIZER;
pthread_cond_t full = PTHREAD_COND_INITIALIZER;
// 商品队列
Queue* productQueue;
// 最大商品个数
const int maxProducts = 10;
// 生产者线程函数
void* producer(void* arg) {
    while (1) {
        sleep(3);
        pthread_mutex_lock(&mutex);
        while (productQueue->size >= maxProducts) {
            pthread_cond_wait(&full, &mutex);
        }
        int newId = rand() % 1000;
        enqueue(productQueue, newId);
        printf("Producer %ld produced product %d\n", (long)arg, newId);
        pthread_cond_signal(&empty);
        pthread_mutex_unlock(&mutex);
    }
    return NULL;
}
// 消费者线程函数
void* consumer(void* arg) {
    sleep(5);
    while (1) {
        sleep(1);
        pthread_mutex_lock(&mutex);
        while (productQueue->size <= 0) {
            pthread_cond_wait(&empty, &mutex);
        }
        int consumedId = dequeue(productQueue);
        printf("Consumer %ld consumed product %d\n", (long)arg, consumedId);
        pthread_cond_signal(&full);
        pthread_mutex_unlock(&mutex);
    }
    return NULL;
}
int main() {
    srand(time(NULL));
    productQueue = createQueue();
    // 初始化商品个数为8
    for (int i = 0; i < 8; i++) {
        int id = rand() % 1000;
        enqueue(productQueue, id);
    }
    pthread_t producerThreads[3];
    pthread_t consumerThreads[2];
    // 创建生产者线程
    for (int i = 0; i < 3; i++) {
        pthread_create(&producerThreads[i], NULL, producer, (void*)(intptr_t)i);
    }
    // 创建消费者线程
    for (int i = 0; i < 2; i++) {
        pthread_create(&consumerThreads[i], NULL, consumer, (void*)(intptr_t)i);
    }
    // 等待生产者线程结束
    for (int i = 0; i < 3; i++) {
        pthread_join(producerThreads[i], NULL);
    }
    // 等待消费者线程结束
    for (int i = 0; i < 2; i++) {
        pthread_join(consumerThreads[i], NULL);
    }
    // 销毁队列
    destroyQueue(productQueue);
    // 销毁互斥锁和条件变量
    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&empty);
    pthread_cond_destroy(&full);
    return 0;
}
